﻿using System;
using System.Collections.Generic;
using System.Linq;
using Xenta.Enums;
using Xenta.Security;
using Xenta.Utils;

namespace Xenta.Operations
{
    public class ExportLookupAttributes : ExportOperation,
        ISecuredOperation
    {
        #region Properties

        public IEnumerable<SecurityPolicy> SecurityPolicies
        {
            get
            {
                yield return new SecurityPolicy()
                    .IsAuthenticated()
                    .HasPermission("SYS");
            }
        }

        #endregion

        #region Utilities

        protected override void Work(IUnitOfWork uow, out IEnumerable<IDictionary<String, String>> result)
        {
            var ret = new List<IDictionary<String, String>>();
            var languages = uow.Execute(new SearchLanguages()).Result;

            foreach(var i in uow.Execute(new SearchLookupAttributes()).Result)
            {
                var obj = new Dictionary<String, String>
                {
                    {"EntityID", i.EntityID.ToString()},
                    {"DisplayOrder", i.DisplayOrder.ToString()},
                    {"Flags", i.Flags.ToString()}
                };
                foreach(var l in languages)
                {
                    var t = i.Translation(l.EntityID);
                    obj.Add(l.Code, t != null ? String.Format("{0}:{1}", t.Name, t.Value) : String.Empty);
                }
                ret.Add(obj);
            }
            result = ret;
        }

        #endregion
    }
}
